﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DoNet.FrameWork.Services.Impl;
using DoNet.FrameWork.Repository;
using DoNet.FrameWork.Repository.Specification;

using Authority.Entity;
using Authority.Repositories;

namespace Authority.Services.Impl
{
    public class UserServiceImpl : ServicesBase<User>, IUserService
    {
        public UserServiceImpl(IUnitOfWork _context, IUserRepository _Repository)
            : base(_context, _Repository)
        {

        }

        /// <summary>
        /// 获取MD5值
        /// </summary>
        /// <param name="key">加密的字符串</param>
        /// <returns>返回MD5值</returns>
        private string HashCode(string key)
        {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(key, "MD5");
        }

        public User CheckUserAndPassword(string userid, string password)
        {
            ISpecification<User> condition = new DirectSpecification<User>(u => u.UserId == userid.Trim());
            User user = repository.GetByCondition(condition);

            if (user != null)
            {
                if (user.Password !=
                    this.HashCode(user.UserId.ToUpper() + password
                    + user.SysCreatedOn.ToLongDateString()))
                {
                    return null;
                }
            }

            return user;
        }


        public void ChangedPassword(User entity, string password)
        {
            entity.Password = this.HashCode(entity.UserId.ToUpper() + password + entity.SysCreatedOn.ToLongDateString());
            entity.SysModifiedBy = entity.Id;
            //entity.SysCreatedOn = DateTime.Now;
            base.Modify(entity);
        }


        public string InsertOrEditUser(User entity)
        {
            ISpecification<User> condition = new DirectSpecification<User>(p => p.Id == entity.Id);
            User user = base.GetByCondition(condition);
            if (user != null)
            {
                user.UserId = entity.UserId;
                user.UserName = entity.UserName;
                user.OrganizeId = entity.OrganizeId;
                user.PostId = entity.PostId;
                user.IsEnabled = true;
                user.Email = entity.Email;
                user.Address = entity.Address;
                user.SysModifiedBy = entity.SysModifiedBy;
                user.SysModifiedOn = entity.SysModifiedOn;
                base.Modify(user);
            }
            else
            {
                entity.Password = this.HashCode(entity.UserId.ToUpper() + "123456" + entity.SysCreatedOn.ToLongDateString());
                base.NewSave(entity);
            }
            return "保存成功！";
        }

        public string DeleteUserById(Guid id)
        {
            ISpecification<User> condition = new DirectSpecification<User>(o => o.Id == id);
            //Post post = base.GetByCondition(condition);
            base.Remove(condition);
            return "删除成功！";
        }


        public string ReSetPasswordById(Guid id)
        {
            ISpecification<User> condition = new DirectSpecification<User>(o => o.Id == id);
            //Post post = base.GetByCondition(condition);
            User user = base.GetByCondition(condition);
            if (user != null)
            {
                user.Password = this.HashCode(user.UserId.ToUpper() + "123456" + user.SysCreatedOn.ToLongDateString());
                base.Modify(user);
            }
            return "密码重置成功！";
        }
    }
}
